cmake_minimum_required(VERSION 3.22)
project(depth_viewer)

find_package(OpenCV REQUIRED)
find_package(CUDA REQUIRED)

set(SOURCES
    webots/controllers/depth_viewer/src/main.cpp
    depth_anything/depth_model.h
    depth_anything/depth_model.cpp
)

set(WEBOTS_HOME "/usr/local/webots" CACHE PATH "Path to the webots installation directory")

# Link with the Webots controller library.
link_directories(${WEBOTS_HOME}/lib/controller)
set(LIBRARIES
    ${CMAKE_SHARED_LIBRARY_PREFIX}Controller${CMAKE_SHARED_LIBRARY_SUFFIX}
    ${CMAKE_SHARED_LIBRARY_PREFIX}CppController${CMAKE_SHARED_LIBRARY_SUFFIX}
)

# Trt libraries
set(TENSORRT_LIBS nvinfer nvinfer_plugin nvparsers nvonnxparser)

include_directories(${WEBOTS_HOME}/include/controller/c ${WEBOTS_HOME}/include/controller/cpp)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/depth_anything)
include_directories(${CUDA_INCLUDE_DIRS})

# Setup the target executable.
add_executable(${CMAKE_PROJECT_NAME} ${SOURCES})
target_link_libraries(${CMAKE_PROJECT_NAME}
    ${LIBRARIES} 
    ${OpenCV_LIBS}
    ${CUDA_LIBRARIES}
    ${TENSORRT_LIBS}
)

# Copy the target executable to the right location.
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E
    copy ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME} 
         ${CMAKE_CURRENT_SOURCE_DIR}/webots/controllers/depth_viewer/depth_viewer
)
